package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AnnualWorkDaysMapper;
import com.ruoyi.business.domain.AnnualWorkDays;
import com.ruoyi.business.service.IAnnualWorkDaysService;

import javax.validation.Validator;

/**
 * 全年出班天数Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class AnnualWorkDaysServiceImpl  extends BaseServiceImpl implements IAnnualWorkDaysService
{
    @Autowired
    private AnnualWorkDaysMapper annualWorkDaysMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询全年出班天数
     * 
     * @param id 全年出班天数主键
     * @return 全年出班天数
     */
    @Override
    public AnnualWorkDays selectAnnualWorkDaysById(Long id)
    {
        return annualWorkDaysMapper.selectAnnualWorkDaysById(id);
    }

    /**
     * 查询全年出班天数列表
     * 
     * @param annualWorkDays 全年出班天数
     * @return 全年出班天数
     */
    @Override
    public List<AnnualWorkDays> selectAnnualWorkDaysList(AnnualWorkDays annualWorkDays)
    {
        return annualWorkDaysMapper.selectAnnualWorkDaysList(annualWorkDays);
    }

    /**
     * 新增全年出班天数
     * 
     * @param annualWorkDays 全年出班天数
     * @return 结果
     */
    @Override
    public int insertAnnualWorkDays(AnnualWorkDays annualWorkDays)
    {
        annualWorkDays.setCreateTime(DateUtils.getNowDate());
        return annualWorkDaysMapper.insertAnnualWorkDays(annualWorkDays);
    }

    /**
     * 修改全年出班天数
     * 
     * @param annualWorkDays 全年出班天数
     * @return 结果
     */
    @Override
    public int updateAnnualWorkDays(AnnualWorkDays annualWorkDays)
    {
        annualWorkDays.setUpdateTime(DateUtils.getNowDate());
        return annualWorkDaysMapper.updateAnnualWorkDays(annualWorkDays);
    }

    /**
     * 批量删除全年出班天数
     * 
     * @param ids 需要删除的全年出班天数主键
     * @return 结果
     */
    @Override
    public int deleteAnnualWorkDaysByIds(Long[] ids)
    {
        return annualWorkDaysMapper.deleteAnnualWorkDaysByIds(ids);
    }

    /**
     * 删除全年出班天数信息
     * 
     * @param id 全年出班天数主键
     * @return 结果
     */
    @Override
    public int deleteAnnualWorkDaysById(Long id)
    {
        return annualWorkDaysMapper.deleteAnnualWorkDaysById(id);
    }

    @Override
    public String importData(List<AnnualWorkDays> userList, boolean updateSupport, String operName) {
    if (userList == null || userList.isEmpty()) {
        throw new ServiceException("导入年度工作日数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
    for (AnnualWorkDays user : userList) {
        try {
            i++;
            if (user.getAwardYear() == null || user.getAwardYear().equals("") || user.getEmployeeId() ==null || user.getEmployeeId().equals("") || !isAwardYearValid(user.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个年度工作日
            AnnualWorkDays annualWorkDays = new AnnualWorkDays();
            annualWorkDays.setEmployeeId(user.getEmployeeId());
            annualWorkDays.setAwardYear(user.getAwardYear());

            List<AnnualWorkDays> selectAnnualWorkDaysList = annualWorkDaysMapper.selectAnnualWorkDaysList(annualWorkDays);

            if (selectAnnualWorkDaysList.isEmpty()) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                // 设置初始值
                user.setCreateTime(DateUtils.getNowDate());
                user.setCreateBy(operName);
                annualWorkDaysMapper.insertAnnualWorkDays(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、年度工作日 "  + " 导入成功");
            } else if (true) {
                // 验证数据
                AnnualWorkDays existingWorkDay = selectAnnualWorkDaysList.get(0);
                BeanValidators.validateWithException(validator, user);
                user.setId(existingWorkDay.getId());
                user.setUpdateTime(DateUtils.getNowDate());
                user.setUpdateBy(operName);
                annualWorkDaysMapper.updateAnnualWorkDays(user);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、年度工作日 "  + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、年度工作日 " +  " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、年度工作日 " + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }
        setCacheInfo("年度工作日",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
